package com.blacksheep.mtm.controller;

import com.blacksheep.mtm.common.utils.ResultUtil;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.common.Result;
import com.blacksheep.mtm.pojo.manager.TbDevice;
import com.blacksheep.mtm.service.DeviceService;
import com.blacksheep.mtm.service.IceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@Api("冰块管理列表")
public class IceController {

    @Autowired
    private IceService iceService;

    @RequestMapping(value = "/ice/in/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取入库列表")
    public DataTablesResult getIceList(int draw, int start, int length, String searchKey, int[] state,
                                            String minDate, String maxDate,String[] type,
                                            @RequestParam("search[value]") String search,
                                            @RequestParam("order[0][column]") int orderCol,
                                            @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","device_code", "create_time"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= iceService.getIceInList(draw,start,length,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }

    @RequestMapping(value = "/ice/out/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取出库列表")
    public DataTablesResult getWSDeviceList(int draw, int start, int length, String searchKey, int[] state,
                                          String minDate, String maxDate,
                                          @RequestParam("search[value]") String search,
                                          @RequestParam("order[0][column]") int orderCol,
                                          @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","device_code", "create_time"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= iceService.getIceOutList(draw,start,length,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }

    @RequestMapping(value = "/ice/in/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增冰块")
    public Result<Integer> insertIn(String codes){
        String[] list = codes.split("\n");
        int result = iceService.inBatch(Arrays.asList(list));
        return new ResultUtil<Integer>().setData(result);
    }
    @RequestMapping(value = "/ice/out/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增冰块")
    public Result<Integer> insertOut(String codes){
        String[] list = codes.split("\n");
        int result = iceService.outBatch(Arrays.asList(list));
        return new ResultUtil<Integer>().setData(result);
    }

    @RequestMapping(value = "/ice/in/del/{ids}",method = RequestMethod.PUT)
    @ApiOperation(value = "下架")
    public Result<TbDevice> stop(@PathVariable Integer[] ids){

        return new ResultUtil<TbDevice>().setData(null);
    }
@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
